import { _decorator, Component, Node, Label, math } from 'cc';
import { VIDEO_MSG, VIDEO_STATUS } from '../../ccc3-framework-core/const/MSG';
import dataUtil from '../../ccc3-framework-core/framework/modules/dataUtil';
import eventUtil from '../../ccc3-framework-core/framework/modules/eventUtil';
import { httpUtil } from '../../ccc3-framework-core/framework/modules/httpUtil';
import { memoryConfig } from '../../ccc3-framework-core/framework/modules/memoryConfig';
import { uiManager } from '../../ccc3-framework-core/framework/modules/uiManager';
import { UiForm } from '../../ccc3-framework-core/framework/ui/uiForm';
import { platformUtil } from '../../ccc3-framework-core/platform/platformUtil';
import { CategoryDefine, eventDefine } from '../CustomDefine';
import TaskProxy from '../../3rd/Guyu/Scripts/[业务]/广告任务/Controller/TaskProxy';
import { LeveLCfg } from '../../sheets/vo/LeveLCfg';
const { ccclass, property } = _decorator;

@ccclass('fatigueForm')
export class fatigueForm extends UiForm {

    @property(Node)
    public btnCancel: Node = null;

    @property(Node)
    public btnVideo: Node = null;

    @property(Label)
    public txtVideoFatigueNum: Label = null;

    start() {
        this.bindTouchEnd(this.btnCancel, this.hideForm)
        this.bindTouchEnd(this.btnVideo, this.onVideo2)
    }

    public onEnable() {
        let video_fatigue_num = memoryConfig.instance.getIntKey("video_fatigue_num", 3);
        this.txtVideoFatigueNum.string = `x ${video_fatigue_num}`;
    }

    private async onVideo2() {
        let taskConfig = TaskProxy.inst.taskConfig;
        let adUnitId = taskConfig.m_CommmonRewardID;
        let ad = Guyu.AdFactory.getAD({
            adUnitId: adUnitId,
            init: true,
        });
        let { success, errMsg } = await ad.playAd({
            showError: true,
            showLoading: true,
        });
        if (!success) {
            uiManager.showToast(errMsg);
            return;
        }
        let video_fatigue_num = memoryConfig.instance.getIntKey("video_fatigue_num", 3);
        let local_fatigue_num = dataUtil.getUnlockNumByDetailId(1, CategoryDefine.FATIGUE);
        let maxNum = 4;
        if (local_fatigue_num >= 0) {
            if (local_fatigue_num + video_fatigue_num > maxNum) {
                let addNum = maxNum - video_fatigue_num;
                if (addNum <= 0)
                    addNum = 0;
                dataUtil.addUnlock(1, CategoryDefine.FATIGUE, addNum, 0);
            }
            else {
                dataUtil.addUnlock(1, CategoryDefine.FATIGUE, video_fatigue_num, 0);
            }
        }
        else {
            dataUtil.setUnlock(1, CategoryDefine.FATIGUE, video_fatigue_num, 0);
        }

        eventUtil.dispatchEvent(eventDefine.FATIGUE_NUM_CHANGED, null);
        this.hideForm();
    }

    private onVideo() {
        httpUtil.reportVideoStart("增加体力");
        platformUtil.currentPlatform.showVideo(res => {
            switch (res) {
                case VIDEO_STATUS.END:
                    httpUtil.reportVideoFinish("增加体力");
                    let video_fatigue_num = memoryConfig.instance.getIntKey("video_fatigue_num", 3);
                    let local_fatigue_num = dataUtil.getUnlockNumByDetailId(1, CategoryDefine.FATIGUE);
                    let maxNum = 4;
                    if (local_fatigue_num >= 0) {
                        if (local_fatigue_num + video_fatigue_num > maxNum) {
                            let addNum = maxNum - video_fatigue_num;
                            if (addNum <= 0)
                                addNum = 0;
                            dataUtil.addUnlock(1, CategoryDefine.FATIGUE, addNum, 0);
                        }
                        else {
                            dataUtil.addUnlock(1, CategoryDefine.FATIGUE, video_fatigue_num, 0);
                        }
                    }
                    else {
                        dataUtil.setUnlock(1, CategoryDefine.FATIGUE, video_fatigue_num, 0);
                    }

                    eventUtil.dispatchEvent(eventDefine.FATIGUE_NUM_CHANGED, null);
                    this.hideForm();
                    break;
                case VIDEO_STATUS.ERR:
                    uiManager.showToast(VIDEO_MSG.ERR);
                    break;
                case VIDEO_STATUS.NOTEND:
                    uiManager.showToast(VIDEO_MSG.NOTEND);
                    break;
                default:
                    break;
            }
        })
    }

    update(deltaTime: number) {

    }
}

